<!DOCTYPE html>
<html>

	<head>
		<meta charset="utf-8">
		<title>公司新闻</title>
		<meta name="viewport" content="width=device-width, maximum-scale = 1, minimum-scale=1" />
		<meta name="apple-mobile-web-app-capable" content="yes" />
		<meta name="viewport" content="width=640,user-scalable=0">
		
		<link rel="stylesheet" href="css/style.css">
		<link rel="stylesheet" href="css/mui.min.css">
		<!--<link rel="stylesheet" href="css/mui.min.css">-->
	</head>

	<body>
		<div id="slider" class="mui-slider">
			<div id="slider_img" class="mui-slider-group mui-slider-loop"></div>
			<div id="slider_indicator" class="mui-slider-indicator"></div>
		</div>
		<div class="clear"></div>

		<!--下拉刷新容器-->
		<div id="pullrefresh" class="mui-content mui-scroll-wrapper">
			<div class="mui-scroll new-list">
				<!--数据列表-->
				<ul class="mui-table-view mui-table-view-chevron">

				</ul>
			</div>
		</div>
		<script src="js/jquery-2.2.3.min.js"></script>
		<script src="js/mui.min.js"></script>
		<script src="js/app.js"></script>
		<script>
			var httpurl = web_url;

			var news_page = 0;

			mui.init({
				pullRefresh: {
					container: '#pullrefresh',
					down: {
						contentnomore: '', //可选，请求完毕若没有更多数据时显示的提醒内容；
						callback: pulldownRefresh
					},
					up: {
						contentrefresh: '',
						contentinit: '',
						contentnomore: '', //可选，请求完毕若没有更多数据时显示的提醒内容；
						callback: pullupRefresh
					}
				}
			});

			mui.plusReady(function() {
				getslider();
				//打开新闻详情
				mui('.mui-table-view').on('tap', '.mui-table-view-cell', function() {
					var news_id = this.id;
					mui.openWindow({
						url: 'news_info.html',
						id: 'news_info.html',
						extras: {
							data_id: news_id
						},

						waiting: {
							autoShow: false, //自动显示等待框
							//title: '正在加载...', //等待对话框上显示的提示内容
						}
					})
				});
			});
			/**
			 * 下拉刷新具体业务实现
			 */
			function pulldownRefresh() {
				news_page = 1;
				setTimeout(function() {
					var table = document.body.querySelector('.mui-table-view');
					var cells = document.body.querySelectorAll('.mui-table-view-cell');
					var postData = {
						apiUrl: httpurl + 'page_news/newslist.shtml',
						datas: {
							page: news_page
						}
					};

					app.postServer(postData,
						function(result) {

							table.innerHTML = '';
							mui.each(result.news, function(index, item) {
								var news_id = item.id,
									img = item.image,
									title = item.name,
									dates = item.date,
									click = item.click;

								var li = document.createElement('li');

								li.className = 'mui-table-view-cell mui-media';
								li.id = news_id;
								li.innerHTML = '<div class="newtu"><img src="' +
									resource_url + img +
									'" style="max-height:120px;"></div><a>' +
									title +
									'</a><span>发布时间：' +
									dates +
									'</span><p><img src="images/liulan.png">' +
									click +
									'</p>';

								table.appendChild(li);
							});

						},

						function(err) {
							if(err) {

								plus.nativeUI.toast(err);
								return;
							}
						}
					);

					mui('#pullrefresh').pullRefresh().endPulldownToRefresh(); //refresh completed
					mui('#pullrefresh').pullRefresh().endPullupToRefresh(false);
				}, 500);
			}

			/**
			 * 上拉加载具体业务实现
			 */
			function pullupRefresh() {

				setTimeout(function() {
					news_page = news_page + 1;
					var table = document.body.querySelector('.mui-table-view');
					var postData = {
						apiUrl: httpurl + 'page_news/newslist.shtml',
						datas: {
							page: news_page
						}
					};

					app.postServer(postData,
						function(result) {

							if(result.news.length == 0) {
								mui('#pullrefresh').pullRefresh().endPullupToRefresh(true);
							} else {
								mui('#pullrefresh').pullRefresh().endPullupToRefresh(false);
							}

							mui.each(result.news, function(index, item) {
								var news_id = item.id,
									img = item.image,
									title = item.name,
									dates = item.date,
									click = item.click;

								var li = document.createElement('li');

								li.className = 'mui-table-view-cell mui-media';
								li.id = news_id;
								li.innerHTML = '<div class="newtu"><img src="' +
									resource_url + img +
									'" style="max-height:120px;"></div><a>' +
									title +
									'</a><span>发布时间：' +
									dates +
									'</span><p><img src="images/liulan.png">' +
									click +
									'</p>';

								table.appendChild(li);
							});

						},

						function(err) {
							if(err) {

								plus.nativeUI.toast(err);
								return;
							}
						}
					);

				}, 1000);
			}
			if(mui.os.plus) {
				mui.plusReady(function() {
					setTimeout(function() {
						mui('#pullrefresh').pullRefresh().pullupLoading();
					}, 500);

				});
			} else {
				mui.ready(function() {
					mui('#pullrefresh').pullRefresh().pullupLoading();
				});
			}


			function getslider() {
				var result;
				var postData = {
					apiUrl: web_url + 'page_news/newslist.shtml',
					datas: {
						num: 1
					}
				};

				app.postServer(postData,
					function(result) {

						var slide_list = $("#slider_img");
						slide_list.html("");

						var div, div_start, div_end, end_index = result.newsbanner.length - 1;

						$.each(result.newsbanner, function(index, item) {

							var img_url = resource_url+ item.image;
							console.log(img_url);
							if(index == 0) {
								div_end = '<div class="mui-slider-item mui-slider-item-duplicate"><a><img src="'
								+img_url + 
								'"></a></div>';
								$("#slider_indicator").append('<div class="mui-indicator mui-active"></div>')
							} else {
								$("#slider_indicator").append('<div class="mui-indicator"></div>')
							}
							div = '<div class="mui-slider-item"><a><img src="' + img_url + '"></a>';
							slide_list.append(div);
							if(index == end_index) {
								div_start = '<div class="mui-slider-item mui-slider-item-duplicate"><a><img src="' +
									img_url + '"></a></div>';
							}
						});

						slide_list.prepend(div_start);
						slide_list.append(div_end);
						var slider = mui("#slider");
						slider.slider({
							interval: 3000
						});
						return;
					},

					function(err) {
						if(err) {

							return 0;
						}
					}
				);
			}
		</script>

	</body>

</html>